#
#
# General settings file
#
# It is best if you do not alter this file, but create a copy with 
# a different name and use that
#
# This file contains settings that one might want to keep
# identical over many analysis and visualization modules.
# 
# The module needs to be imported as
#
# from settings import *
#
# so that it populates the importing module's namespace.
#
# It is best if you place (a renamed copy) of this file
# in the same directory as the importing module (and import 
# it under the new name)
#
import os, sys
import atlas
from atlas import join_paths, sql
import functools

# default home location
home = atlas.ENV.HOME_DIR 

#
# Overwrite existing labels. Setting this to false means that
# data may not be accidentally overwritten, but it also means
# that you cannot rerun the same analysis on the same database.
#
CLOBBER = True

#
# The size of the data vector ( the larger the better, but uses more memory).
# No need for it to be larger than the largest chromosome size.
# We use 1 million for yeast, 10 million for drosophila, 30 million for human.
#
DATA_SIZE = 3*10**6

#
# Minimum peak height (larger values make for fewer stored data points and faster execution).
# It also affects fitting, the fit will not extend for the points
# below this value (this will be the base value). For large genomes this 
# can substantially speed up execution. 
#
MINIMUM_PEAK_SIZE = 0.1

#
# Typically these directories do not need to be altered.
# Files placed in the DOWNLOAD_DIR will be downloadable
# via the webserver.
#
TEMPLATE_DIR = join_paths( home, "html" )
SESSION_DIR  = join_paths( home, "html", "session" )
STATIC_DIR   = join_paths( home, "html", "static" ) 
DOWNLOAD_DIR = join_paths( home, "html", "static", "download" )
IMAGE_DIR    = join_paths( home, "html", "static", "img" ) 
STATIC_URL   = "/static"

#
# Socket related configuration.
#
SOCKET_PORT = 8080
SOCKET_HOST = "localhost"
THREAD_POOL = 10

#
# A number of convenience functions that make configuration easier 
# to manage, typically you don't need to edit below it
#
def get_experiment_dir(name, path=atlas.ENV.LIBRARY_DIR):
    "Convenience function to retrieve the database directory"
    return join_paths( path, name )

def get_hdf_file(path, name):
    "Convenience function to generate hdf file name"
    return join_paths( path, "%s-data.hdf" % name )

def get_sqlite_uri(path, name):
    "Convenience function to generate sqlite database URI"
    fullpath = join_paths( path, "%s-data.sqlite" % name )
    return "sqlite:///%s" % fullpath

def get_fit_label(label, sigma):
    "Fit label generator"
    return "%s-SIGMA-%s" % (label, sigma )

def get_pred_label(label, sigma):
    "Peak label generator"
    return "PRED-%s-SIGMA-%s" % (label, sigma )

def load_feature_files( sql_uri, path, fnames, clobber=False, drop_indices=False):
    """
    Loads features from file names
    """
    # drop sql indices (speeds up bulk inserting) 
    engine = sql.get_engine( sql_uri )
    
    if drop_indices:
        sql.drop_indices(engine)

    for fname in fnames:
        fname =  join_paths( path, fname )
        sql.load_features( sql_uri, fname, clobber=clobber, drop=False )

    if drop_indices:
        sql.create_indices(engine)

def get_chromosomes( fname, label ):
    "Returns the chromosomes for a certain label"
    try:
        from atlas import hdf
        db = hdf.hdf_open( fname, mode='r' )
        data = hdf.GroupData(db=db, name=label)
        labels = data.labels
        db.close()
        return [ (x, x) for x in labels ]

    except Exception, exc:
        atlas.error("unable to read chromosomes %s" % exc )
        sys.exit()

def get_zoomlevels( levels ):
    "Returns the zoom levels formatted in a more 'humane' way"
    humanized = map(atlas.commify, map(str, levels))
    return zip(levels, humanized)

def fill_params ( func, label, sigma, **kwds):
    """
    Partially fill a function signature for a typical label/fit/prediction
    type of plot
    """
    fit_label  = get_fit_label(label, sigma)
    pred_label = get_pred_label(label, sigma)
    return functools.partial ( 
        func, data_label = label, 
        fit_label  = fit_label,
        pred_label = pred_label, **kwds)
